
/**
 * 队列处理微信消息通知设备以及用户
 */
class TaskQueue {
    constructor(concurrency) {
        this.concurrency = concurrency;
        this.running = 0;
        this.queue = [];
    }

    /**
     * 添加处理任务
     */
    pushTask(task) {
        this.queue.push(task);
        this.next();
    }

    next(){
        while(this.running < this.concurrency && this.queue.length) {
            const task = this.queue.shift();
            task(() => {
                this.running --;
                this.next();
            });
            this.running++;
        }
    }
}

export default TaskQueue;